<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/blackboxsurvey/lib.php');
require_once($CFG->dirroot . "/local/blackboxsurvey/forms.php");
define('AJAX_SCRIPT', true);

//yeu cau login,sau khi login tu dong chuyen sang trang chu
global $USER;
if ($USER->id == null){
    $SESSION->homepage = true;
    require_login($SITE);
}
if($SESSION->homepage){
    $SESSION->homepage = null;
    redirect( $CFG->wwwroot);
}

//object class
require_once($CFG->dirroot . "/local/blackboxsurvey/class/survey.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questions.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/question_choice.php");

//path
$path = $CFG->wwwroot.'/local/blackboxsurvey/admin/manage_survey.php';
$path_image = $CFG->wwwroot .'/local/blackboxsurvey/image/';

//require_login($SITE);
//require_capability('local/blackboxsurvey:adminmanagement', context_system::instance());

//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

/* Chuan bi trang web can hien thi */	
$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('Survey Form');
$PAGE->set_heading('Survey Form');
$PAGE->set_pagelayout('standard');

echo $OUTPUT->header();

$id = required_param('id', PARAM_TEXT);

/* Viet doan code cua ban o day */
//cho nay goi cai dong ham lay survey
$class_survey = new b_survey();
$survey = $class_survey->get_survey_byID($id);
$b_ques = new b_questions();
$b_ques_choice = new b_question_choice();
//lay cau hoi da add vao survey
$questions = $class_survey->get_survey_question_bySurveyID($id);

//gan nhung du lieu vao $arry de hien tren form
$array['program'] = $survey->program_name;
$array['id'] = $id;
$array['start'] = $survey->date_start;
$array['end'] = $survey->date_end;
//luu lai tong cong bao nhieu cau hoi
$array['ques_num'] = count($questions);
$count = 1;
foreach($questions as $q) {
    $q_content = preg_replace(array('#<p>#', '#</p>#'), '', $q->content, 1);
    $array['ques_content'.$count] = $count.'- '.$q_content;
    //goi ham lay tat ca choice cua cau hoi
    $ques_choice = $b_ques_choice->get_choice_by_question($q->ques_id);
    //dem so luong option
    $array['num_choice'.$count] = count($ques_choice);
    $n = 1;
    //chay foreach de gan choice vao array
    $array['type'.$count] = $q->id_type;
    foreach($ques_choice as $c){
        $array['choice_id'.$count.$n] = $c->id;
        $array['choice_content'.$count.$n] = $c->content;
        $n++;
    }
    $count++;  
}
//Lay guide in
$array['guide_in'] = $survey->guide_input;
//Lay guide out
$array['guide_out'] = $survey->guide_output;
//Lay print
$array['print_line'] = $survey->print_line;
//array('id'=>$type_id,'path_image'=>$path_image)
$mform = new form_review_survey(null, $array);

if ($fromform = $mform->get_data()) 
{	
	//close colorbox
	echo "<script>parent.$.fn.colorbox.close(); </script>";
}


$mform->display();
/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();
?>